package com.zzj.controller;

import com.zzj.base.Result;
import com.zzj.model.dto.MessageDTO;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class MailController {

  @Resource
  private JavaMailSender mailSender;

  @Operation(summary = "发送右键", description = "目标邮箱 标题 内容")
  @PostMapping("sendMessage")
  public Result sendMessage(@RequestBody @Validated MessageDTO in) {
    String to = in.getTo();
    String title = in.getTitle();
    String content = in.getContent();
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("wesley@zzjtpp.top");
    message.setTo(to);
    message.setSubject(title);
    message.setText(content);
    mailSender.send(message);
    return Result.success();
  }

  @GetMapping("test")
  public String test() {
    return "test";
  }
}
